package com.mystyle.le.liuxiang;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.mystyle.le.liuxiang.fragment.EntertainmentFragment;
import com.mystyle.le.liuxiang.fragment.Franco_americanFragment;
import com.mystyle.le.liuxiang.fragment.GossipFragment;
import com.mystyle.le.liuxiang.fragment.InteriorFragment;
import com.mystyle.le.liuxiang.fragment.Japan_KoreaFragment;
import com.mystyle.le.liuxiang.fragment.VideoFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private String [] titls = {"娱乐头条","视频","内地","日韩","欧美","港台"};
    private RadioButton[] rads;
    private ViewPager viewPager;
    private List<Fragment> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    @TargetApi(Build.VERSION_CODES.M)
    private void initView() {
        radioGroup = ((RadioGroup) findViewById(R.id.radio));
        rads = new RadioButton[titls.length];
        for (int i =0;i<titls.length;i++){
            rads[i] = new RadioButton(MainActivity.this);
            Bitmap b = null;
            rads[i].setButtonDrawable(new BitmapDrawable(b));
            rads[i].setTextSize(20);
            rads[i].setBackgroundResource(R.drawable.radiobutton_color);
            rads[i].setGravity(Gravity.CENTER);
            int screenWith = getResources().getDisplayMetrics().widthPixels;
            int eachWith = screenWith/titls.length;
            rads[i].setWidth(eachWith);
            rads[i].setText(titls[i]);
            radioGroup.addView(rads[i]);
        }
        rads[0].setChecked(true);


        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i =0;i<titls.length;i++){
                    if (rads[i].getId()==checkedId){
                        viewPager.setCurrentItem(i);
                    }
                }
            }
        });

        viewPager = ((ViewPager) findViewById(R.id.viewPager));


        list.add(new EntertainmentFragment());
        list.add(new Franco_americanFragment());
        list.add(new GossipFragment());
        list.add(new InteriorFragment());
        list.add(new Japan_KoreaFragment());
        list.add(new VideoFragment());

        viewPager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager()));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
               rads[position].setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class MyFragmentAdapter extends FragmentPagerAdapter{

        public MyFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}
